package com.ruoyi.demo.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.demo.dto.TaskDataDto;
import com.ruoyi.demo.mapper.ProTaskSnDataMapper;
import com.ruoyi.demo.vo.SnPrintDataVo;
import com.ruoyi.demo.vo.TaskDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.domain.MesBindTaskData;
import com.ruoyi.demo.service.IMesBindTaskDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务单记录数据Controller
 *
 * @author shc
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/api/MesBindTaskData")
public class MesBindTaskDataController extends BaseController {
    @Autowired
    private IMesBindTaskDataService mesBindTaskDataService;

    @Autowired
    private ProTaskSnDataMapper proTaskSnDataMapper;

    /**
     * 查询任务单记录数据列表
     */
//    @PreAuthorize("@ss.hasPermi('demo:MesBindTaskData:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesBindTaskData mesBindTaskData) {
        startPage();
        List<MesBindTaskData> list = mesBindTaskDataService.selectMesBindTaskDataList(mesBindTaskData);
        return getDataTable(list);
    }


    /**
     * 获取任务单记录数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('demo:MesBindTaskData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesBindTaskDataService.getById(id));
    }

    /**
     * 新增任务单记录数据
     */
    @PreAuthorize("@ss.hasPermi('demo:MesBindTaskData:add')")
    @Log(title = "任务单记录数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesBindTaskData mesBindTaskData) {
        return toAjax(mesBindTaskDataService.save(mesBindTaskData));
    }

    /**
     * 修改任务单记录数据
     */
    @PreAuthorize("@ss.hasPermi('demo:MesBindTaskData:edit')")
    @Log(title = "任务单记录数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesBindTaskData mesBindTaskData) {
        return toAjax(mesBindTaskDataService.updateById(mesBindTaskData));
    }

    /**
     * 删除任务单记录数据
     */
    @PreAuthorize("@ss.hasPermi('demo:MesBindTaskData:remove')")
    @Log(title = "任务单记录数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mesBindTaskDataService.removeByIds(Arrays.asList(ids)));
    }

    @PostMapping("/getSnRankApi")
    public AjaxResult getSnRankApi(@RequestBody TaskDataDto taskDataDto) {
        if (taskDataDto == null) {
            return AjaxResult.error("传参为空");
        }
        List<TaskDataVo> snRank = mesBindTaskDataService.getSnRank(taskDataDto);
        return AjaxResult.success(snRank);
    }

    @Log(title = "任务单记录数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    public void export(HttpServletResponse response, TaskDataDto taskDataDto) throws Exception {
        System.out.println(taskDataDto);

        taskDataDto.setSnList(Arrays.asList(taskDataDto.getSns().split("\n")));
        List<TaskDataVo> list = mesBindTaskDataService.getSnRank(taskDataDto);
        System.out.println(list);
        ExcelUtil<TaskDataVo> util = new ExcelUtil<TaskDataVo>(TaskDataVo.class);
        util.exportExcel(response, list, "AB档数据");
    }


}
